package com.ruoyi.web.controller.weixin;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.weixin.domain.WxMenu;
import com.ruoyi.weixin.service.IWxMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 自定义菜单Controller
 *
 * @author dqianhe
 * @date 2022-04-27
 */
@RestController
@RequestMapping("/weixin/menu")
public class WxMenuController extends BaseController {
    @Autowired
    private IWxMenuService wxMenuService;

    /**
     * 查询自定义菜单列表
     */
    @PreAuthorize("@ss.hasPermi('weixin:menu:list')")
    @GetMapping("/list")
    public TableDataInfo list(WxMenu wxMenu) {
        startPage();
        List<WxMenu> list = wxMenuService.selectWxMenuList(wxMenu);
        return getDataTable(list);
    }

    /**
     * 导出自定义菜单列表
     */
    @PreAuthorize("@ss.hasPermi('weixin:menu:export')")
    @Log(title = "自定义菜单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxMenu wxMenu) {
        List<WxMenu> list = wxMenuService.selectWxMenuList(wxMenu);
        ExcelUtil<WxMenu> util = new ExcelUtil<WxMenu>(WxMenu.class);
        util.exportExcel(response, list, "自定义菜单数据");
    }

    /**
     * 获取自定义菜单详细信息
     */
    @PreAuthorize("@ss.hasPermi('weixin:menu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(wxMenuService.selectWxMenuById(id));
    }

    /**
     * 新增自定义菜单
     */
    @PreAuthorize("@ss.hasPermi('weixin:menu:add')")
    @Log(title = "自定义菜单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxMenu wxMenu) {
        return toAjax(wxMenuService.insertWxMenu(wxMenu));
    }

    /**
     * 修改自定义菜单
     */
    @PreAuthorize("@ss.hasPermi('weixin:menu:edit')")
    @Log(title = "自定义菜单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxMenu wxMenu) {
        return toAjax(wxMenuService.updateWxMenu(wxMenu));
    }

    /**
     * 删除自定义菜单
     */
    @PreAuthorize("@ss.hasPermi('weixin:menu:remove')")
    @Log(title = "自定义菜单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(wxMenuService.deleteWxMenuByIds(ids));
    }
}
